package com.desksoft.ecommerce.jfreechart;

import java.awt.Font;
import java.io.IOException;
import java.io.OutputStream;

import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;


/***************************************
 * <pre>
 * 版权所有：杭州迪科软件 保留所有权利
 * 创建日期: Aug 17, 2011 4:42:44 PM
 * 创建作者：luoy
 * 文件名称：JFreeChart帮助类
 * 版本：
 * 功能：
 * 最后修改时间：Aug 17, 2011 4:42:44 PM
 * 修改记录：
 * </pre>
 *****************************************/
public class JFreeChartUtil {

	public final static Font FONT_SONT = new Font("宋体", Font.BOLD, 15); 
	
	/**
	 * 导出形式
	 */
	public final static int OUT_PNG = 0;
	public final static int OUT_JPEG = 1;
	public final static int OUT_HTTP = 2;
	
	/**
	 * 图片默认长宽
	 */
	public final static int DEFAULT_WIDTH = 1000;
	public final static int DEFAULT_HEIGHT = 500;
	
	/**
	 * 设置字体
	 */
	public static void setFont(JFreeChart chart, Font font){
		CategoryPlot plot = (CategoryPlot) chart.getPlot();    
		
		chart.getTitle().setFont(font);
		chart.getLegend().setItemFont(font);

		// 设置Y轴字体
        plot.getDomainAxis().setLabelFont(font);
        plot.getDomainAxis().setTickLabelFont(font);
        
        // 设置X轴字体
        plot.getRangeAxis().setLabelFont(font);
        plot.getRangeAxis().setTickLabelFont(font);
        
        // 背景色 透明度     
        plot.setBackgroundAlpha(0.5f);
        // 前景色 透明度     
        plot.setForegroundAlpha(1f);     
	}
	
	/**
	 * 折线图显示折点
	 */
	public static void displayLineChartExtremePoint(JFreeChart chart){
		CategoryPlot plot = (CategoryPlot) chart.getPlot();
		LineAndShapeRenderer   renderer   =   (LineAndShapeRenderer)plot.getRenderer();
		renderer.setShapesVisible(true);
		renderer.setDrawOutlines(true);
		renderer.setUseFillPaint(true);
		renderer.setFillPaint(java.awt.Color.white); 
	}
	
	/**
	 * 折线图显示折点数值
	 */
	public static void displayLineChartPointData(JFreeChart chart, Font font){
		CategoryPlot plot = (CategoryPlot) chart.getPlot();
		LineAndShapeRenderer   renderer   =   (LineAndShapeRenderer)plot.getRenderer();
		renderer.setBaseItemLabelGenerator (new StandardCategoryItemLabelGenerator());
		renderer.setItemLabelFont (font);
		renderer.setItemLabelsVisible (true);
	}
	
	/**
	 * 统计图导出
	 * @throws IOException 
	 */
public static void output(JFreeChart chart, OutputStream out, int type) throws IOException{
		
		switch (type){
			case OUT_PNG:{
				ChartUtilities.writeChartAsPNG(out, chart, DEFAULT_WIDTH, DEFAULT_HEIGHT);
				break;
			}
			case OUT_JPEG:{
				ChartUtilities.writeChartAsJPEG(out, chart, DEFAULT_WIDTH, DEFAULT_HEIGHT);
				break;
			}
			case OUT_HTTP:{
				ChartUtilities.writeChartAsPNG(out, chart, DEFAULT_WIDTH, DEFAULT_HEIGHT);
				break;
			}
		}
	}
}
